// -*- coding: utf-8 -*-
/*
 *   This is the "function" file storing methods to get AQI data from pm25.in
 * 
 *   Author: William Wong <librehat@outlook.com>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library General Public License as
 *   published by the Free Software Foundation; either version 3 or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

import QtQuick 1.1 

Item {
    id: pm25
    
    function query(city) {
        if(city == "")
            return //fail silently

        var url = 'http://www.pm25.in/api/querys/aqi_details.json?token=pEwKXAJHnHGu4DQ3En5v&city=' + city;
        var resText;
        var doc = new XMLHttpRequest();
        doc.onreadystatechange = function() {
            if (doc.readyState == XMLHttpRequest.DONE) {
                resText = doc.responseText;
                getData(resText);
            }
        }
        doc.open('GET', url, true);
        doc.send();
    }
    
    function getData(resText) {
        var jsonObj = JSON.parse(resText);
        
        if (typeof jsonObj == 'object') {
            mainWindow.error = "";//Clean first.
            mainWindow.m_stations = "";
            mainWindow.m_aqis = "";
            mainWindow.m_qualitys = "";
            mainWindow.m_ppollutants = "";
            mainWindow.m_pm2_5s = "";
            mainWindow.m_pm10s = "";
            mainWindow.m_cos = "";
            mainWindow.m_no2s = "";
            mainWindow.m_so2s = "";
            mainWindow.m_o38hs = "";
            
            if (typeof jsonObj.error == 'string')
                mainWindow.error = i18n("Error:") + " " + jsonObj.error;
            else {
                mainWindow.m_cityname = jsonObj[0].area;
                for (var i = 0; i < jsonObj.length; i++) {
                    if (i+1 == jsonObj.length) {//The last station is "avg"
                        mainWindow.m_avgAQI = jsonObj[i].aqi;
                        mainWindow.m_avgQuality = jsonObj[i].quality;
                        mainWindow.m_avgPollutant = jsonObj[i].primary_pollutant;
                        mainWindow.m_avgpm25 = jsonObj[i].pm2_5;
                        mainWindow.m_timePoint = jsonObj[i].time_point;
                    }
                    else {
                        mainWindow.m_stations += jsonObj[i].position_name + "<br />";
                        mainWindow.m_aqis += jsonObj[i].aqi + "<br />";
                        mainWindow.m_qualitys += jsonObj[i].quality + "<br />"
                        mainWindow.m_ppollutants += jsonObj[i].primary_pollutant + "<br />"
                        mainWindow.m_pm2_5s += jsonObj[i].pm2_5 + "<br />"
                        mainWindow.m_pm10s += jsonObj[i].pm10 + "<br />"
                        mainWindow.m_cos += jsonObj[i].co + "<br />"
                        mainWindow.m_no2s += jsonObj[i].no2 + "<br />"
                        mainWindow.m_so2s += jsonObj[i].so2 + "<br />"
                        mainWindow.m_o38hs += jsonObj[i].o3_8h + "<br />";
                    }
                }
            }
        }
        else {
            mainWindow.error = i18n("Unknown Error.<br />It's probably a network error.");
        }
    }
    
    function rank() {
        console.log("Getting Rank...")
        var resText;
        var doc = new XMLHttpRequest();
        doc.onreadystatechange = function() {
            if (doc.readyState == XMLHttpRequest.DONE) {
                resText = doc.responseText;
                getRank(resText);
            }
        }
        doc.open('GET', 'http://www.pm25.in/api/querys/aqi_ranking.json?token=pEwKXAJHnHGu4DQ3En5v', true);
        doc.send();
    }
    
    function getRank(resText) {
        var jsonObj = JSON.parse(resText);
        var cityFound = false;
        
        if (typeof jsonObj == 'object' && mainWindow.m_cityname != "") {
            //clean first
            mainWindow.m_rankNum = "";
            mainWindow.m_rankCities = "";
            mainWindow.m_rankAQIs = "";
            mainWindow.m_rankQuality = "";
            mainWindow.m_rankLevel = "";
            mainWindow.m_rankPPollutants = "";
            mainWindow.m_rankPM25_24h = "";
            mainWindow.m_rankPM10_24h = "";
            
            if (typeof jsonObj.error == 'string')//output error in console
                console.log("Error: " + jsonObj.error);
            
            else {
                
                for (var i = 0; i < 10; i++) {
                    if (jsonObj[i].area == mainWindow.m_cityname) {
                        cityFound = true;
                        mainWindow.m_rankNum += "<b>" + (i+1) + "</b><br />";
                        mainWindow.m_rankCities += "<b>" + jsonObj[i].area + "</b><br />";
                        mainWindow.m_rankAQIs += "<b>" + jsonObj[i].aqi + "</b><br />";
                        mainWindow.m_rankQuality += "<b>" + jsonObj[i].quality + "</b><br />";
                        mainWindow.m_rankLevel += "<b>" + jsonObj[i].level + "</b><br />";
                        mainWindow.m_rankPPollutants += "<b>" + jsonObj[i].primary_pollutant + "<b><br />";
                        mainWindow.m_rankPM25_24h += "<b>" + jsonObj[i].pm2_5_24h + "</b><br />";
                        mainWindow.m_rankPM10_24h += "<b>" + jsonObj[i].pm10_24h + "</b><br />";
                    }
                    else {
                        mainWindow.m_rankNum += (i+1) + "<br />";
                        mainWindow.m_rankCities += jsonObj[i].area + "<br />";
                        mainWindow.m_rankAQIs += jsonObj[i].aqi + "<br />";
                        mainWindow.m_rankQuality += jsonObj[i].quality + "<br />";
                        mainWindow.m_rankLevel += jsonObj[i].level + "<br />";
                        mainWindow.m_rankPPollutants += jsonObj[i].primary_pollutant + "<br />";
                        mainWindow.m_rankPM25_24h += jsonObj[i].pm2_5_24h + "<br />";
                        mainWindow.m_rankPM10_24h += jsonObj[i].pm10_24h + "<br />";
                    }
                }
                
                if (!cityFound) {
                    for (var i = 10; i < jsonObj.length - 10; i++) {
                        if (jsonObj[i].area == mainWindow.m_cityname) {
                            cityFound = true;
                            mainWindow.m_rankNum += "<b>" + (i+1) + "</b><br />";
                            mainWindow.m_rankCities += "<b>" + jsonObj[i].area + "</b><br />";
                            mainWindow.m_rankAQIs += "<b>" + jsonObj[i].aqi + "</b><br />";
                            mainWindow.m_rankQuality += "<b>" + jsonObj[i].quality + "</b><br />";
                            mainWindow.m_rankLevel += "<b>" + jsonObj[i].level + "</b><br />";
                            mainWindow.m_rankPPollutants += "<b>" + jsonObj[i].primary_pollutant + "<b><br />";
                            mainWindow.m_rankPM25_24h += "<b>" + jsonObj[i].pm2_5_24h + "</b><br />";
                            mainWindow.m_rankPM10_24h += "<b>" + jsonObj[i].pm10_24h + "</b><br />";
                        }
                    }
                }
                
                for (var i = jsonObj.length - 10; i < jsonObj.length; i++) {
                    if (jsonObj[i].area == mainWindow.m_cityname) {
                        cityFound = true;
                        mainWindow.m_rankNum += "<b>" + (i+1) + "</b><br />";
                        mainWindow.m_rankCities += "<b>" + jsonObj[i].area + "</b><br />";
                        mainWindow.m_rankAQIs += "<b>" + jsonObj[i].aqi + "</b><br />";
                        mainWindow.m_rankQuality += "<b>" + jsonObj[i].quality + "</b><br />";
                        mainWindow.m_rankLevel += "<b>" + jsonObj[i].level + "</b><br />";
                        mainWindow.m_rankPPollutants += "<b>" + jsonObj[i].primary_pollutant + "<b><br />";
                        mainWindow.m_rankPM25_24h += "<b>" + jsonObj[i].pm2_5_24h + "</b><br />";
                        mainWindow.m_rankPM10_24h += "<b>" + jsonObj[i].pm10_24h + "</b><br />";
                    }
                    else {
                        mainWindow.m_rankNum += (i+1) + "<br />";
                        mainWindow.m_rankCities += jsonObj[i].area + "<br />";
                        mainWindow.m_rankAQIs += jsonObj[i].aqi + "<br />";
                        mainWindow.m_rankQuality += jsonObj[i].quality + "<br />";
                        mainWindow.m_rankLevel += jsonObj[i].level + "<br />";
                        mainWindow.m_rankPPollutants += jsonObj[i].primary_pollutant + "<br />";
                        mainWindow.m_rankPM25_24h += jsonObj[i].pm2_5_24h + "<br />";
                        mainWindow.m_rankPM10_24h += jsonObj[i].pm10_24h + "<br />";
                    }
                }
            }
        }
    }
}